<?php if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
include_once APPPATH.'modules/social/OAuth_Facebook.php';
class facebook_api{

	private $vendor='facebook';
	private $pz;
	protected $oauth;
	protected $client;
	protected $info;
	function __construct(){
		$socialapis = load_api('setting')->getSetting('socialapi');
		$this->info = $socialapis['facebook'];
		$this->pz = &get_instance();
		$this->oauth = new Facebook(array(
				'appId'  => $this->info['appkey'],
				'secret' => $this->info['appsecret'],
		));
	}

	function getloginpage($state=NULL,$display=NULL){
		return $this->oauth->getLoginUrl(array('redirect_uri' => $this->info['callback'],
				'scope' => 'email',
               	'req_perms' => 1,
               	'fbconnect' => 0));
	}

	function get_accesstoken(){
		return true;
	}

	function get_userinfo(){
		$user = $this->oauth->getUser();
		if($user){
			try {
				$user_profile = $this->oauth->api('/me');
				$userinfo = array();
				$userinfo['uid'] = $user_profile['id'];
				$userinfo['screen_name'] = $user_profile['username'];
				$userinfo['email'] = $user_profile['email'];
				$userinfo['name'] = $user_profile['name'];
				$userinfo['avatar'] = "https://graph.facebook.com/".$user_profile['id']."/picture";
				$userinfo['location'] = $user_profile['location']?$user_profile['location']['name']:'';
				$userinfo['description'] = $user_profile['bio'];
				$userinfo['url'] = $user_profile['link'];
				if($user_profile['gender']=='male'){
					$userinfo['gender'] = 'male';
				}elseif ($temp['gender']=='female'){
					$userinfo['gender'] = 'female';
				}else{
					$userinfo['gender'] = 'none';
				}
				return $userinfo;

			} catch (FacebookApiException $e) {
				$errot = $e;
			}
		}
		return null;
	}
}
